import type { ApiScheduleTypes } from "@/api/api-types/api-schedule-types";
import { ORDER_ADDRESS_TYPE } from "../enum/pd-enum";
import Api from "@/api/api-declare";
import Store from "@/store";

/**
 * 行程操作相关公共
 */
export function useSchedueCtrl() {
	//创建行程入参
	const createScheduleReq = reactive<ApiScheduleTypes.ScheduleCreateReq>({
		depart_area: {
			type_id: ORDER_ADDRESS_TYPE.depart,
			province: '',
			city: '',
			district: '',
			address: '定位中，定位失败请手动选择出发地',
			latitude: '',
			longitude: '',
		},
		arrive_area: {
			type_id: ORDER_ADDRESS_TYPE.arrive,
			province: '',
			city: '',
			district: '',
			address: '',
			latitude: '',
			longitude: '',
		},
		start_time: 0,
		seat_num : Store.driver().maxSeatNum,
		vacancy: 4,
	});

	//创建行程
	const createSchedule = (): Promise<{id : number}> => {
		return new Promise((r) => {
			Api.ApiSchedule.createSchedule(createScheduleReq).then(res => {
				r({
					id : res.id
				});
			});
		});
	};

	//行程列表
	const scheduleList = ref<ApiScheduleTypes.ScheduleListItem[]>([]);
	//获取行程列表
	const getScheduleList = () : Promise<void> => {
		return new Promise((r) => {
			Api.ApiSchedule.getScheduleList().then(res => {
				scheduleList.value = res.list;
				r();
			});
		});
	};

	//行程详情
	const scheduleInfo = ref<ApiScheduleTypes.GetScheduleInfoRes>();
	const getScheduleInfo = (id : number) : Promise<void> => {
		return new Promise((r) => {
			Api.ApiSchedule.getScheduleInfo({id}).then(res => {
				scheduleInfo.value = res;
				r();
			});
		});
	};

	return {
		createScheduleReq,
		createSchedule,
		scheduleList,
		getScheduleList,
		scheduleInfo,
		getScheduleInfo
	};
}